<?php
class Fe_Customer extends CI_Controller{

	function __construct() {
		parent::__construct ();
		$this->load->library('form_validation');
		$this->load->model('be/customer_model','model');
		$this->load->helper('form');
		$this->load->helper('url');
	}

	function logout(){
		$this->session->sess_destroy();
		redirect(base_url().'index.php/admin/login');
	}

	function register() {
		$this->form_validation->set_rules ( 'fullname', 'Họ tên','required');
		$this->form_validation->set_rules ( 'username', 'Tên tài khoản', 'required|min_lenght[5]|max_lenght[20]|is_unique[customers.username]');
		$this->form_validation->set_rules ( 'password', 'Mật khẩu', 'required|matches[password]' );
		$this->form_validation->set_rules ( 'email', 'Email', 'required|valid_email|is_unique[customers.email]' );
		$this->form_validation->set_rules ( 'phone', 'Điện thoại','required' );
		$this->form_validation->set_rules ( 'address', 'Địa chỉ' , 'required' );
		$this->form_validation->set_rules ( 'district', 'Quận/Huyện', 'required' );
		$this->form_validation->set_rules ( 'province', 'Tỉnh/Thành phố', 'required' );
		$this->form_validation->set_rules ( 'active_status', 'Trạng thái', 'required' );
		$this->form_validation->set_rules ( 'avatar', 'Hình ảnh', 'callback_get_avatar_uploaded' );
		$this->form_validation->set_rules ( 'group', 'Nhóm khách hàng', 'required' );
		$this->form_validation->set_rules ( 'description', 'Mô tả' );
		
		if ($this->form_validation->run () == FALSE) {
			$data ['page_title'] = 'Thêm mới khách hàng';
			$data ['main_content'] = 'fe/customer/register';
			$this->load->view ( 'fe/includes/template', $data );
		} else {
			$data ['name'] = $this->input->post ( 'fullname' );
			$data ['username'] = $this->input->post ( 'username' );
			$data ['password'] = md5($this->input->post ( 'password' ));
			$data ['email'] = $this->input->post ( 'email' );
			$data ['phone'] = $this->input->post ( 'phone' );
			$data ['address'] = $this->input->post ( 'address' );
			$data ['district'] = $this->input->post ( 'district' );
			$data ['province'] = $this->input->post ( 'province' );
			$data ['active_status'] = $this->input->post ( 'active_status' );
			$data ['group'] = $this->input->post ( 'group' );
			$data ['description'] = $this->input->post ( 'description' );
			
			$data ['avatar'] = $this->avatar;
			
			if (! $this->model->add ( $data )) {
				$data ['page_title'] = 'Có lỗi xảy ra';
				$mess_detail = '<p>Hãy kiểm tra lại thông tin bạn đã điền vào form!</p>';
				$mess_detail .= '<p><a href="' . base_url () . 'index.php/customer/register"> Làm lại</a> ';
				$data ['message_detail'] = $mess_detail;
			} else {
				$data ['page_title'] = 'Thành công';
				$mess_detail = '<p>Chúc mừng bạn đã đăng ký thành công!</p>';
				$mess_detail .= '<p><a href="' . base_url () . 'index.php/fe_customer/login">Login</a>';
				$data ['message_detail'] = $mess_detail;
			}
			$data ['main_content'] = 'fe/message';
			$this->load->view ( 'fe/includes/template', $data );
		}
	}


/////////////////////////////////////
	function home(){
		$data['page_title'] = 'Overview';
		$data['main_content'] = 'fe/home';					
		$this->load->view ( 'fe/includes/template', $data );
	}


	function login(){
		if($this->session->userdata('customername') != '') {
			$this->home();
		}
		else {
			$this->form_validation->set_rules('username','Tên đăng nhập','trim|required');
			$this->form_validation->set_rules('password','Mật khẩu','trim|required');
			//$this->session->sess_destroy();
			if ($this->form_validation->run () == FALSE){
				$data ['page_title'] = 'Login';
				$data ['main_content'] = 'fe/customer/login';
				$this->load->view ( 'fe/includes/template', $data );
			}
			else{
				$userdata['username']=$this->input->post('username');
				$userdata['password']=$this->input->post('password');
				$query = $this->model->validate($userdata);
				if($query){
					$customer_id = $this->model->get_by_username($this->input->post('username'));
					$user_data = array(
						'customername' => $this->input->post('username'),
						'id' => $customer_id,
						'is_logged_in' => true
					);
			
					$this->session->set_userdata($user_data);
					$data['page_title'] = 'Overview';
					$data['main_content'] = 'fe/cardnet';					
					$this->load->view ( 'be/includes/template', $data );
				}
				else{
					//$this->login();
					$data ['page_title'] = 'Sorry';
					$data['message_detail'] = '<p>Tên đăng nhập hoặc mật khẩu không đúng!</p>';
					$data['message_detail'] .= '<p><a href="' . base_url () . 'index.php/fe_customer/login" >Đăng nhập</p>';
					$data ['main_content'] = 'fe/message';
					$this->load->view ( 'fe/includes/template', $data );
				}
			}
		}
	}

	function get_avatar_uploaded() {
		//Create location if it does not exist
		$location = DEFAULT_IMAGE_UPLOAD_PATH . '/customer';
		if (! is_dir ( $location )) {
			if (! mkdir ( $location, 0755 )) {
				die ( 'Failed to create folders...' );
			}
		}
		$config ['upload_path'] = $location;
		$config ['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
		$config ['max_size'] = '10000';
		
		$this->load->library ( 'upload', $config );
		if ($this->upload->do_upload ( 'avatar' )) {
			$tmp = $this->upload->data ( 'avatar' );
			$this->avatar = $location . '/' . $tmp ['file_name'];
		} else {
			$this->avatar =  CUSTOMER_DEFAULT_AVATAR;
		}
	}

	function update() {		
		$id = $this->session->userdata('id');
		$customer = $this->model->get($id);
				
		$data['id'] = $id;
		$data ['name'] = $customer->name;
		$data ['username'] = $customer->username;
		$data ['password'] = $customer->password;
		$data ['email'] = $customer->email;
		$data ['phone'] = $customer->phone;
		$data ['address'] = $customer->address;
		$data ['district'] = $customer->district;
		$data ['province'] = $customer->province;
		$data ['active_status'] = $customer->active_status;
		$data ['avatar'] = $customer->avatar;
		$data ['group'] = $customer->group;
		$data ['description'] = $customer->description;
		
		
		$this->form_validation->set_rules ( 'fullname', 'Họ tên' );
		$this->form_validation->set_rules ( 'username', 'Tên tài khoản' );
		$this->form_validation->set_rules ( 'password', 'Mật khẩu' );
		$this->form_validation->set_rules ( 'email', 'Email' );
		$this->form_validation->set_rules ( 'phone', 'Điện thoại' );
		$this->form_validation->set_rules ( 'address', 'Địa chỉ' );
		$this->form_validation->set_rules ( 'district', 'Quận/Huyện' );
		$this->form_validation->set_rules ( 'province', 'Tỉnh/Thành phố' );
		$this->form_validation->set_rules ( 'active_status', 'Trạng thái' );
		$this->form_validation->set_rules ( 'avatar', 'Hình ảnh', 'callback_get_avatar_uploaded' );
		$this->form_validation->set_rules ( 'group', 'Nhóm khách hàng' );
		$this->form_validation->set_rules ( 'description', 'Mô tả' );
		
		if ($this->form_validation->run () == FALSE) {
			$data ['page_title'] = 'Cập nhật thông tin khách hàng';
			$data ['main_content'] = 'be/customer/update';
			$this->load->view ( 'be/includes/template', $data );
		} else {
			$data ['name'] = $this->input->post ( 'fullname' );
			$data ['username'] = $this->input->post ( 'username' );
			$data ['password'] = $this->input->post ( 'password' );
			$data ['email'] = $this->input->post ( 'email' );
			$data ['phone'] = $this->input->post ( 'phone' );
			$data ['address'] = $this->input->post ( 'address' );
			$data ['district'] = $this->input->post ( 'district' );
			$data ['province'] = $this->input->post ( 'province' );
			$data ['active_status'] = $this->input->post ( 'active_status' );
			$data ['group'] = $this->input->post ( 'group' );
			$data ['description'] = $this->input->post ( 'description' );
			
			if($this->avatar == CUSTOMER_DEFAULT_AVATAR ) {
				$data ['avatar'] = $customer->avatar;
			}else {
				$data['avatar'] = $this->avatar;
			}					
			if (! $this->model->update ($id, $data )) {
				$data ['page_title'] = 'Có lỗi xảy ra';
				$mess_detail = '<p>Hãy kiểm tra lại thông tin bạn đã điền vào form!</p>';
				$mess_detail .= '<p><a href="' . base_url () . 'index.php/customer/update"> Làm lại</a> hoặc <a href="' . base_url () . 'index.php/customer">Trở lại trang danh sách</a></p>';
				$data ['message_detail'] = $mess_detail;
			} else {
				$data ['page_title'] = 'Thành công';
				$mess_detail = '<p>Cập nhật thành công!</p>';
				$mess_detail .= '<p><a href="' . base_url () . 'index.php/customer/update">Tiếp tục</a> hoặc <a href="' . base_url () . 'index.php/customer">Trở lại trang danh sách</a>';
				$data ['message_detail'] = $mess_detail;
			}
			$data ['main_content'] = 'be/customer/message';
			$this->load->view ( 'be/includes/template', $data );
		}
	}
}
?>